package com.nbsaas.boot.shiro3.thread;

import java.util.concurrent.Executor;

public class ContextAwareExecutor implements Executor {
    private final Executor executor;

    public ContextAwareExecutor(Executor executor) {
        this.executor = executor;
    }

    @Override
    public void execute( Runnable command) {
        if (command==null){
            return;
        }
        Long tenantId = TenantThreadLocal.getTenant();
        executor.execute(() -> {
            TenantThreadLocal.setTenant(tenantId);  // 在执行任务之前设置上下文
            try {
                command.run();
            } finally {
                TenantThreadLocal.clear();  // 清除上下文，防止泄漏
            }
        });
    }
}
